package com.easyctrl.ldy.view;

import java.util.ArrayList;

import com.easyctrl.db.DeviceManager;
import com.easyctrl.db.FloorManager;
import com.easyctrl.db.RoomManager;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.domain.DeviceBean;
import com.easyctrl.ldy.domain.FloorBean;
import com.easyctrl.ldy.domain.RoomBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class BottomPositionView extends LinearLayout {

	private LayoutInflater mInflater;
	private LinearLayout layout;
	private Button btnFloor;
	private Button btnRoom;
	private Button btnModule;
	
	private FloorManager floorManager;
	private RoomManager roomManager;
	private DeviceManager deviceManager;
	
	private SelectPositionPopupWindow popupWindow;
	public SelectPositionPopupWindow getPopupWindow() {
		return popupWindow;
	}
 
	public BottomPositionView(Context context) {
		super(context);  
		
		floorManager = FloorManager.getInstance(context);
		roomManager = RoomManager.getInstance(context);
		deviceManager = DeviceManager.getInstance(context);
		
		mInflater = LayoutInflater.from(getContext());
		layout = (LinearLayout) mInflater.inflate(R.layout.bottom_position_select, this);
		
		btnFloor = (Button) layout.findViewById(R.id.floor);
		btnRoom = (Button) layout.findViewById(R.id.room);
		btnModule = (Button) layout.findViewById(R.id.module);
		
		btnFloor.setOnClickListener(new BottomOnClickListener());
		btnRoom.setOnClickListener(new BottomOnClickListener());
		btnModule.setOnClickListener(new BottomOnClickListener());
		
	//	popupWindow = SelectPositionPopupWindow.getInstance(context);
		popupWindow = new SelectPositionPopupWindow(context);
	} 
	
	private class BottomOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) { 
			if(R.id.floor == v.getId()){ 
				popupWindow.show(v, getContentByFloor(floorManager.findAll()));
			}else if(R.id.room == v.getId()){
				popupWindow.show(v, getContentByRoom(roomManager.findAll()));
			}else if(R.id.module == v.getId()){
				popupWindow.show(v, getContentByDevice(deviceManager.findAll()));
			}
		} 
	}
	
	public ArrayList<String> getContentByFloor(ArrayList<FloorBean> list){
		ArrayList<String> arrs = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			arrs.add(list.get(i).name);
		}
		return arrs;
	}
	
	public ArrayList<String> getContentByRoom(ArrayList<RoomBean> list){
		ArrayList<String> arrs = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			arrs.add(list.get(i).name);
		}
		return arrs;
	}
	
	public ArrayList<String> getContentByDevice(ArrayList<DeviceBean> list){
		ArrayList<String> arrs = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			arrs.add(list.get(i).device_name);
		}
		return arrs;
	}
}
